请问vue组件如何reload或者说vue

您所在的位置:网站首页 vue $route 请问vue组件如何reload或者说vue

请问vue组件如何reload或者说vue

2023-04-03 13:28| 来源: 网络整理| 查看: 265

利用v-if控制router-view,在路由容器组件,如APP.vue中实现一个刷新方法

export default { data () { return { isRouterAlive: true } }, methods: { reload () { this.isRouterAlive = false this.$nextTick(() => (this.isRouterAlive = true)) } } }

然后其它任何想刷新自己的路由页面,都可以这样:

this.$root.reload()

如果$root节点不是路由容器组件,可以使用provide / inject来传递reload

路由容器组件:

export default { provide () { return { reload: this.reload } }, data () { return { isRouterAlive: true } }, methods: { reload () { this.isRouterAlive = false this.$nextTick(() => (this.isRouterAlive = true)) } } }

子组件:

... { inject: ['reload'] } ... this.reload()

--------- 更新 ---------

再补充一下,其实基于上述原理,你可以封装一个 reload 组件,然后其他组件通过 Mixins 的方式再引入它,这样就可以很方便地支持任意组件级别的 reload 了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3